import 'package:flutter/services.dart';
import 'package:get/get.dart';

class CardReader {

  /// 单例
  /// 单例
  static final CardReader _instance = CardReader._internal();

  factory CardReader() => _instance;

  CardReader._internal();

  static const MethodChannel _channel = MethodChannel('card_reader');

  /// 打开读卡器设备
  /// 返回设备句柄
  static Future<int> _open() async {
    try {
      final result = await _channel.invokeMethod<int>('open');
      if (result == null) {
        throw Exception('打开读卡器失败: 返回空结果');
      }
      return result;
    } on PlatformException catch (e) {
      throw Exception('打开读卡器失败: ${e.message}');
    } catch (e) {
      e.printError();
      throw Exception('打开读卡器失败: $e');
    }
  }

  /// 关闭读卡器设备
  /// [handle] 设备句柄
  static Future<int> _close(int handle) async {
    if (handle <= 0) {
      throw Exception('无效的设备句柄: $handle');
    }
    try {
      final result = await _channel.invokeMethod<int>('close', {'handle': handle});
      if (result == null) {
        throw Exception('关闭读卡器失败: 返回空结果');
      }
      return result;
    } on PlatformException catch (e) {
      throw Exception('关闭读卡器失败: ${e.message}');
    } catch (e) {
      throw Exception('关闭读卡器失败: $e');
    }
  }

  // ... existing code ...
  /// 返回卡片UID
  static Future<Map<String, String>> readUid() async {
    try {
      final handle = await CardReader._open();
      final result = await _channel.invokeMethod<Map<Object?, Object?>>('readUid');
      if (result == null) {
        throw Exception('读取UID失败: 返回空结果');
      }

      // 将结果转换为正确的类型
      final Map<String, String> resultMap = {};
      result.forEach((key, value) {
        if (key is String && value is String) {
          resultMap[key] = value;
        }
      });
      // await CardReader._close(handle);
      return resultMap;
    } on PlatformException catch (e) {
      throw Exception('读取UID失败: ${e.message}');
    } catch (e) {
      throw Exception('读取UID失败: $e');
    } finally {
    }
  }

// ... existing code ...

  /// 蜂鸣
  /// [handle] 设备句柄
  /// [time] 蜂鸣时间
  /// 返回操作结果
  static Future<int> beep() async {
    try {
      final handle = await CardReader._open();
      final result = await _channel.invokeMethod<int>('beep', {'handle': handle, 'time': 30});
      if (result == null) {
        throw Exception('蜂鸣失败: 返回空结果');
      }
      // await CardReader._close(handle);
      return result;
    } on PlatformException catch (e) {
      throw Exception('蜂鸣失败: ${e.message}');
    } catch (e) {
      e.printError();
      throw Exception('蜂鸣失败: $e');
    }
  }

  /// 设置日志监听器
  static void setupLogListener() {
    _channel.setMethodCallHandler((call) async {
      try {
        if (call.method == 'log') {
          // 处理日志消息
          print('CardReader Log: ${call.arguments}');
        }
      } catch (e) {
        print('处理日志消息时出错: $e');
      }
    });
  }
}

// CardReader.setupLogListener();
// final decrypt = AESCBCUtil.decrypt('A363A751C21E7BB8DDF60B59362D2BDA');
// print('decrypt: $decrypt');
// final readUid = await CardReader.readUid();
// print('readUid: $readUid');
